import { _decorator, Button, Component, Node } from 'cc';
import { UIBase } from './UIBase';
import { GameDefine } from '../game/GameDefine';
import { LotteryItem } from './item/LotteryItem';
import { FortressManager } from '../fortress/FortressManager';
import Utils from '../utils/Utils';
import { AudioManager } from '../game/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('UILottery')
export class UILottery extends UIBase {
    public static Instance: UILottery = null!;
    @property(Button) btn_close: Button = null!;
    @property(Node) role1: Node = null!;
    @property(Node) role2: Node = null!;
    @property(Node) role3: Node = null!;
    @property(Button) btn_text: Button = null!;
    @property(Node) lightNode: Node = null!;

    onLoad() {
        super.onLoad();
        UILottery.Instance = this;
        this.btn_close.interactable = false;
    }

    start() {
        this.btn_close.node.on(Button.EventType.CLICK, this.onHide, this);
        Utils.AutoScale(this.btn_close.node);
        this.btn_text.node.on(Button.EventType.CLICK, () => {
            this.startLottery(1);
        }, this);
    }

    onShow(): void {
        super.onShow();
        this.startLottery(GameDefine.FortressLevel);
    }

    onHide(): void {
        this.lightNode.active = false;
        AudioManager.Instance.playUIClick();
        super.onHide(true);
        this.btn_close.interactable = false;
        FortressManager.Instance.onLevelUp(() => {
            console.log('要塞升级完成回调')
            GameDefine.isGamePaused = false;
        });
    }

    /** 开始抽奖 */
    private startLottery(index: number) {
        // 将所有卡牌先重置
        let targetNode: Node = null;
        if (index == 1) {
            this.role1.active = true;
            this.role2.active = false;
            this.role3.active = false;
            targetNode = this.role1;
        }

        if (index == 2) {
            this.role1.active = false;
            this.role2.active = true;
            this.role3.active = false;
            targetNode = this.role2;
        }
        if (index == 3) {
            this.role1.active = false;
            this.role2.active = false;
            this.role3.active = true;
            targetNode = this.role3;
        }

        if (targetNode === null) {
            console.error("抽奖角色节点未找到");
            return;
        }

        this.lightNode.active = false;
        GameDefine.isGamePaused = true;
        this.btn_close.interactable = false;
        let itemList = targetNode.getComponentsInChildren(LotteryItem);
        itemList.forEach(item => { item.hideAward() });
        const interval = 0.05;
        const totalSteps = itemList.length;
        itemList.forEach((item, idx) => {
            this.scheduleOnce(() => {
                item.showAward();
            }, interval * (itemList.length + idx));
        });
        this.scheduleOnce(() => {
            this.lightNode.active = true;
            AudioManager.Instance.playReward();
            this.btn_close.interactable = true;
        }, interval * (totalSteps + 1));

        // // 找到中奖节点
        // const awardIdx = itemList.findIndex(item => item.isAward);
        // if (awardIdx !== -1) {
        //     // 继续高亮到中奖节点（每步0.1s），最后停留
        //     // 上一轮最后高亮的节点索引
        //     const lastIdx = (totalSteps - 1) % itemList.length;
        //     // 计算需要补几步到中奖节点
        //     let stepToAward = (awardIdx - lastIdx + itemList.length) % itemList.length;
        //     if (stepToAward === 0) stepToAward = itemList.length; // 如果正好重合，需再转一圈
        //     for (let i = 1; i <= stepToAward; i++) {
        //         const idx = (lastIdx + i) % itemList.length;
        //         this.scheduleOnce(() => {
        //             itemList.forEach(item => item.lightNode.active = false);
        //             itemList[idx].showLight(lightDuration);
        //         }, interval * (totalSteps + i - 1));
        //     }
        //     // 最后停留在中奖节点
        //     this.scheduleOnce(() => {

        //         console.log("抽中节点索引:" + awardIdx);
        //         itemList.forEach(item => {
        //             item.showAward();
        //         })
        //     }, interval * (totalSteps + stepToAward + 1));
        // }

    }
}



